package per.zqx.rabbit.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * OrderService,
 *
 * @author Sa, Zhang 2021-06-06 14:49
 */

@Service
public class OrderService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void makeOrderDeadQueueFanout(String uid, String pid, int num) {
        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[fanout_ttl_Dead_order_exchange] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "fanout_ttl_Dead_order_exchange";
        String routerKey = "";
        rabbitTemplate.convertAndSend(exchange, routerKey, orderId);
    }

    public void makeOrderTtlQueueFanout(String uid, String pid, int num) {
        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[ttl_4_queue_fanout] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "fanout_ttl_order_exchange";
        String routerKey = "";
        rabbitTemplate.convertAndSend(exchange, routerKey, orderId);
    }

    public void makeOrderTtlSingleFanout(String uid, String pid, int num) {
        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[ttl_4_single_fanout] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "fanout_order_exchange";
        String routerKey = "";
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchange, routerKey, orderId, messagePostProcessor);
    }

    public void makeOrderFanout(String uid, String pid, int num) {

        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[fanout] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "fanout_order_exchange";
        String routerKey = "";
        rabbitTemplate.convertAndSend(exchange, routerKey, orderId);
    }

    public void makeOrderDirect(String uid, String pid, int num) {

        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[direct] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "direct_order_exchange";
        rabbitTemplate.convertAndSend(exchange, "sms", orderId);
        rabbitTemplate.convertAndSend(exchange, "email", orderId);
    }

    public void makeOrderTopic(String uid, String pid, int num) {
        String orderId = UUID.randomUUID().toString();
        System.out.println("OrderService[topic] ==> 订单生成成功，订单号 -> " + orderId);
        String exchange = "topic_order_exchange";
        // 	#.sms.#
        // 	#.email.#
        // #.sms.#
        String routerKey1 = "order.sms.message";
        String routerKey2 = "order.duanxin.message";
        rabbitTemplate.convertAndSend(exchange, routerKey1, orderId);
        rabbitTemplate.convertAndSend(exchange, routerKey2, orderId);
    }

}
